import { defineStore } from 'pinia'
import { ref } from 'vue'

export interface Facility {
  id: string
  name: string
  location: string
  description: string
  image: string // base64格式的图片
  status: 'active' | 'maintenance' | 'closed'
  openTime: string
  closeTime: string
  category: string
}

export const useFacilitiesStore = defineStore(
  'facilities',
  () => {
    // 设施列表
    const facilities = ref<Facility[]>([
      {
        id: '1',
        name: '互动多媒体展示屏',
        location: '一楼展厅',
        description: '触摸屏互动设备，可浏览博物馆藏品和历史信息',
        image: '',
        status: 'active',
        openTime: '09:00',
        closeTime: '17:00',
        category: '电子设备'
      },
      {
        id: '2',
        name: '3D全息投影仪',
        location: '二楼特展区',
        description: '展示文物的3D全息影像，提供沉浸式观赏体验',
        image: '',
        status: 'active',
        openTime: '09:30',
        closeTime: '16:30',
        category: '投影设备'
      },
      {
        id: '3',
        name: '自助导览机',
        location: '各楼层入口',
        description: '提供博物馆地图和展品信息查询服务',
        image: '',
        status: 'maintenance',
        openTime: '09:00',
        closeTime: '17:00',
        category: '服务设施'
      }
    ])

    // 当前选中的设施
    const currentFacility = ref<Facility | null>(null)

    // 搜索关键词
    const searchKeyword = ref('')

    // 根据关键词搜索设施
    const searchedFacilities = () => {
      if (!searchKeyword.value) return facilities.value

      const keyword = searchKeyword.value.toLowerCase()
      return facilities.value.filter(facility =>
        facility.name.toLowerCase().includes(keyword) ||
        facility.description.toLowerCase().includes(keyword) ||
        facility.location.toLowerCase().includes(keyword) ||
        facility.category.toLowerCase().includes(keyword)
      )
    }

    // 设置搜索关键词
    function setSearchKeyword(keyword: string) {
      searchKeyword.value = keyword
    }

    // 设置当前选中的设施
    function setCurrentFacility(facility: Facility | null) {
      currentFacility.value = facility
    }

    // 添加设施
    function addFacility(facility: Omit<Facility, 'id'>) {
      const newId = String(Date.now())
      facilities.value.push({
        ...facility,
        id: newId
      })
      return newId
    }

    // 更新设施信息
    function updateFacility(id: string, updatedInfo: Partial<Facility>) {
      const index = facilities.value.findIndex(f => f.id === id)
      if (index !== -1) {
        facilities.value[index] = {
          ...facilities.value[index],
          ...updatedInfo
        }
        return true
      }
      return false
    }

    // 删除设施
    function deleteFacility(id: string) {
      const index = facilities.value.findIndex(f => f.id === id)
      if (index !== -1) {
        facilities.value.splice(index, 1)
        if (currentFacility.value?.id === id) {
          currentFacility.value = null
        }
        return true
      }
      return false
    }

    // 获取设施详情
    function getFacilityById(id: string) {
      return facilities.value.find(f => f.id === id) || null
    }

    return {
      facilities,
      currentFacility,
      searchKeyword,
      searchedFacilities,
      setSearchKeyword,
      setCurrentFacility,
      addFacility,
      updateFacility,
      deleteFacility,
      getFacilityById
    }
  },
  {
    // 启用持久化存储
    persist: true
  }
)
